package com.xxx.service.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xxx.bean.Demo;
import com.xxx.bean.User;
import com.xxx.bean.VideoTime;
import com.xxx.mapper.DemoMapper;
import com.xxx.multids.DynamicSwitchDataSource;
import com.xxx.service.DemoService;
import com.xxx.util.CommonUtil;
import com.xxx.util.JsonUtil;

/**
 * demo service 接口实现类
 */
@Service
@DynamicSwitchDataSource
public class DemoServiceImpl implements DemoService {

	private static final Logger logger = LoggerFactory.getLogger(DemoServiceImpl.class);

	@Autowired
	private DemoMapper demoMapper;

	@Override
	public List<Demo> getAllRecords(Integer orgId) {
		logger.info("enter method getAllRecords().");
		return demoMapper.getAllRecords();
	}

	@Override
	public String testResult() {
		return "test";
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public int testTx() {
		logger.info("enter method testTx().");
		// 1.保存ssmdemo
		Demo demo = new Demo();
		demo.setsId(7);
		demo.setsName("demo7");
		demo.setsText("demo7text");
		demo.setsAge(7);
		int m = demoMapper.save(demo);
		if (m > 0) {
			logger.info("testTx | save success.");
			// 2. 保存ssmdemo2
			Demo demo2 = new Demo();
			demo2.setsId(3);
			demo2.setsName("demo3");
			demo2.setsAge(3);
			int n = demoMapper.save2(demo2);
			if (n > 0) {
				logger.info("testTx | save2 success.");
			}
			return n;
		}

		return 0;
	}

	@Override
	public int doVideo() {
		List<VideoTime> list = demoMapper.qryVideoTimeList();
		if (list == null || list.isEmpty()) {
			return 0;
		}
		int cnt = 0;
		VideoTime bean;
		for (VideoTime vt : list) {
			String src = vt.getSrc();
			try {
				bean = JsonUtil.decodeJson(src, VideoTime.class);
				vt.setVideoId(bean.getVideoId());
				vt.setTime(bean.getTime());
				vt.setSeconds(CommonUtil.getSeconds(bean.getTime()));
				logger.info(vt.toString());
				demoMapper.updateVideoTime(vt);
			} catch (Exception e) {
				logger.error(src + "发生异常：", e);
				try {
					src = src.substring(1, src.length() - 1);
					String[] arr = src.split(":");
					String vedioId = arr[0];
					String secondStr = arr[1];
					vedioId = vedioId.substring(1, vedioId.length() - 1);
					secondStr = secondStr.substring(1, secondStr.length() - 1);
					vt.setVideoId(vedioId);
					vt.setTime(secondStr);
					vt.setSeconds(Long.valueOf(secondStr));
					logger.info(vt.toString());
					demoMapper.updateVideoTime(vt);
				} catch (Exception e1) {
					continue;
				}
			}
			cnt++;
		}

		return cnt;
	}

	@Override
	public User getUserById(int id) {
		return demoMapper.getUserById(id);
	}

	@Override
	public int insertUser(User user) {
		return demoMapper.insertUser(user.getId(), user.getName(), user.getAge(), user.getSex(), user.getCardno());
	}

}
